package sw6.digipecs.helpers;

import java.io.File;
import java.io.IOException;

import android.content.Context;
import android.media.MediaRecorder;
import android.os.Environment;
import android.widget.Toast;

public class SoundRecorder {
	private Context mContext;
	private static SoundRecorder mInstance;
	private MediaRecorder mMediaRecorder; 
	private String mPath; 
	protected SoundRecorder(Context c)
	{
		mContext = c; 
	}
	public static SoundRecorder getinstance(Context context)
	{
		if(mInstance!=null)
			return mInstance; 
		else 
		{
			mInstance = new SoundRecorder(context); 
			return mInstance; 
		}
	}
	public void startRecording(int imgId)
	{
		File file = new File(Environment.getExternalStorageDirectory().getPath()+"/"+Integer.toString(imgId)+".3gp"); 

		mPath = file.getPath(); 
		mMediaRecorder=new MediaRecorder(); 
		mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
		mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
		mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
		
		mMediaRecorder.setOutputFile(mPath);

		try {
			mMediaRecorder.prepare();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		mMediaRecorder.start(); 
		Toast.makeText(mContext, "start recording", 1000).show(); 
	}
	public String stopRecording()
	{
		mMediaRecorder.stop(); 
		mMediaRecorder.release(); 
		Toast.makeText(mContext, "stop recording", 1000).show(); 
		return mPath; 
	}
}
